package cn.bjxy.shishaolong.system.controller;


import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.common.StatusCode;
import cn.bjxy.shishaolong.common.SysConst;
import cn.bjxy.shishaolong.system.annotation.Permission;
import cn.bjxy.shishaolong.system.entity.Role;
import cn.bjxy.shishaolong.system.service.IRoleService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 时少龙
 * @since 2019-03-31
 */
@RestController
@RequestMapping("/api/admin/rs/role")
@CrossOrigin
public class RoleController {


    @Autowired
    private IRoleService roleService;

    /**
     * 那条件查询
     *
     * @param param
     * @return
     */
    @RequestMapping("/queryByCondition")
    @Permission
    public ResultMsg queryByCondition(@RequestBody JSONObject param) {
        try {
            //查询条件
            Role role = null;
            if (param.containsKey(SysConst.QUERY_CONDITION_KEY)) {
                role = param.getObject(SysConst.QUERY_CONDITION_KEY, Role.class);
            }
            //分页查询
            if (param.containsKey(SysConst.PAGA_CURRENT_KEY) && param.containsKey(SysConst.PAGA_SIZE_KEY)) {
                IPage<Role> page = new Page<>();
                page.setCurrent(param.getLong(SysConst.PAGA_CURRENT_KEY)).setSize(param.getLong(SysConst.PAGA_SIZE_KEY));
//                    IPage<Role> list = professionService.queryByCondition(page, role);
                List<Role> list = roleService.list(null);
                return ResultMsg.success(list);
            } else {  //普通查询
                List<Role> list = roleService.list();
                return ResultMsg.success(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * 角色资源权限分配
     *
     * @param param
     * @return
     */
    @PostMapping("/resourceAuth")
    @Permission
    public ResultMsg addResourceAuth(@RequestBody JSONObject param) {
        try{
            List<Integer> resourceIds = param.getJSONArray("resourceIds").toJavaList(Integer.class);
            Integer roleId = param.getObject("roleId", Integer.class);
            Map<String, Object> delMap=null;
            if(resourceIds!=null && resourceIds.size()>0 && roleId!=null){
                delMap = new HashMap<>();
                delMap.put("role_id", roleId);
            }
            roleService.resourceAuth(delMap,resourceIds,roleId);
            return ResultMsg.success();
        }catch(Exception e){
            e.printStackTrace();
            return ResultMsg.create(StatusCode.SAVE_FAILURE,StatusCode.SAVE_FAILURE_MSG);
        }
    }


    /**
     * 保存
     *
     * @param profession
     * @return
     */
       /* @PostMapping("/save")
        public ResultMsg save(@RequestBody Profession profession) {
            try {
                if (profession == null) {
                    return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
                }
                // 给修改日期赋值
                profession.setUpdateAt(LocalDateTime.now());
                professionService.saveOrUpdate(profession);
                return ResultMsg.success();
            } catch (Exception e) {
                e.printStackTrace();
                return ResultMsg.create(StatusCode.SAVE_FAILURE, StatusCode.SAVE_FAILURE_MSG);
            }
        }

        *//**
     * 删除
     *
     * @param param
     * @return
     *//*
        @DeleteMapping("/delect")
        public ResultMsg delect(@RequestBody JSONObject param) {
            try {
                if (param == null || !param.containsKey(SysConst.BATCH_ID)
                        || !param.getString(SysConst.BATCH_ID).contains(SysConst.BATCH_ID_SEPARATOR)) {
                    return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
                }
                String ids = param.getString(SysConst.BATCH_ID);
                List idlist = Utils.idsToList(ids, SysConst.BATCH_ID_SEPARATOR);
                professionService.removeByIds(idlist);
//            sysAdminService.remove(ids);
                return ResultMsg.success();
            } catch (Exception e) {
                e.printStackTrace();
                return ResultMsg.create(StatusCode.DELETE_FAILUER, StatusCode.DELETE_FAILUER_MSG);
            }
        }

        *//**
     * 获取分配课程的数据
     *
     * @param param
     * @return
     *//*
        @RequestMapping("/getAssinCourseData")
        public ResultMsg getAssinCourseData(@RequestBody JSONObject param) {
            try {
                String professionId = param.getString(SysConst.ID);
                if (StringUtils.isEmpty(professionId)) {
                    return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
                }
                AssinCourse assinCourseData = professionService.getAssinCourseData(professionId);
                return ResultMsg.success(assinCourseData);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
            }
        }

        *//**
     * 保存分配课程的数据
     *
     * @param assinCourse
     * @return
     *//*
        @RequestMapping("/saveAssinCourseData")
        public ResultMsg saveAssinCourseData(@RequestBody AssinCourse assinCourse) {
            try {
                if (Objects.isNull(assinCourse)) {
                    return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
                }
                // 保存分配的课程
                professionService.saveAsignCourse(assinCourse);
                return ResultMsg.success();
            } catch (Exception e) {
                e.printStackTrace();
                return ResultMsg.create(StatusCode.SAVE_FAILURE, StatusCode.SAVE_FAILURE_MSG);
            }
        }

        *//**
     * 获取专业和课程的对应关系
     *
     * @return 返回所有专业的数组
     *//*
        @RequestMapping("/gerProfessionCourseMap")
        public ResultMsg getProfessionCourseMap() {
            try {
                List<ProfessionCourseRelation> list = relationService.list();
                return ResultMsg.success(list);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
            }
        }

        *//**
     * 获取所有专业课程，课程专业对应关系
     * @return
     *//*
        @RequestMapping("/getAllProfessionCourse")
        public ResultMsg getAllProfessionCourse(){
            try {
                List<Course> courseList = courseService.list();
                List<Profession> professionList = professionService.list();
                List<ProfessionCourseRelation> relationList = relationService.list();

                // 把查询的结果放到map集合
                HashMap<String, List> allProfessionCourse = new HashMap<>();
                allProfessionCourse.put(ALL_COURSE_LIST,courseList);
                allProfessionCourse.put(ALL_PROFESSION_LIST,professionList);
                allProfessionCourse.put(ALL_RELATION_LIST,relationList);
                return ResultMsg.success(allProfessionCourse);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
            }
        }*/

}
